package com.example.demo.controller;


import com.example.demo.annotation.RepeatForm;
import com.example.demo.common.enums.QuestionEnum;
import com.example.demo.entity.Comment;
import com.example.demo.service.impl.CommentServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ak
 * @since 2020-04-02
 */
@Controller
@Slf4j
public class CommentController {
    @Autowired
    CommentServiceImpl commentService;

    /**
     * 一级评论
     * @param comment 获取参数列表
     * @param questionId 一级评论所评论的问题
     */
    @GetMapping("firstComment/{questionId}")
    @RepeatForm
    public String firstComment(Comment comment, @PathVariable Integer questionId){
        comment.setCQuestionId(questionId);
        comment.setCType(QuestionEnum.FIRST_COMMENT.getCode());
        log.info(comment.toString());
        commentService.insertComment(comment);
        return "redirect:/question/"+questionId;
    }

    /**
     * 二级评论
     * @param comment 获取参数列表
     * @param questionId 二级评论所评论的评论
     */
    @GetMapping("secondComment/{questionId}")
    @RepeatForm
    public String secondComment(Comment comment, @PathVariable Integer questionId,HttpServletRequest request){
        comment.setCQuestionId(questionId);
        comment.setCType(QuestionEnum.SECOND_COMMENT.getCode());
        log.info(comment.toString());
        commentService.insertComment(comment);
        return "redirect:/question/"+questionId;
    }

}
